<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">

<!--创建模态框-->
<div class="modal fade" id="modal-create">
    <div class="modal-dialog" style="width: 800px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">物料类别维护</h4>
            </div>
            <div class="modal-body">
                <form id="createForm" class="form-horizontal" role="form">
                    <input type="hidden" id="dicId" name="dicId">
                    <input type="hidden" id="dicPid" name="dicPid">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">类别名称</label>
                        <div class="col-sm-7">
                            <input type="text" class="form-control" name="dicName" placeholder="请输入类别名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">类别值</label>
                        <div class="col-sm-7">
                            <input type="text" class="form-control" name="dicValue" placeholder="请输入类别值">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">类别</label>
                        <div class="col-sm-7">
                            <input type="number" class="form-control" name="dicType" placeholder="请输入类别">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">备注</label>
                        <div class="col-sm-7">
                            <input type="text" class="form-control" name="dicReamrk" placeholder="请输入备注">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">状态</label>
                        <div class="col-sm-7">
                            <select name="dicStatus" class="form-control">
                                <option value="1" selected>可用</option>
                                <option value="2">失效</option>
                                <option value="">--请选择--</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">排序</label>
                        <div class="col-sm-7">
                            <input type="number" class="form-control" name="dicOrderNo" placeholder="排序">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-info" id="btn-create">提交</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<section class="content-header">
    <h1>
        物料类别维护
    </h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" onsubmit="return false;">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group" style="margin-right: 10px;">
                                    <label>名称</label>
                                    <input type="text" name="dicName" class="form-control" placeholder="请输入类别名称">
                                </div>
                                <div class="form-group" style="margin-right: 10px;">
                                    <label>状态</label>
                                    <select name="dicStatus" class="form-control">
                                        <option value="1">可用</option>
                                        <option value="2">失效</option>
                                        <option value="" selected>全部</option>
                                    </select>
                                </div>
                                <button type="button" class="btn btn-success" id="btn-search">
                                    <!--<i class="fa fa-search"></i>-->
                                    <span class="Bold">搜索</span>
                                </button>
                                <a href="javascript:void(0);" class="btn btn-info pull-right"
                                   onclick="openCreate()">
                                    <!--<i class="fa fa-plus"></i>-->
                                    <span class="Bold">添加</span>
                                </a>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="box-body">
                    <table id="bootstrap-tree-table"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    var table;

    /**
     * 打开添加模态框
     */
    function openCreate(pid) {
        if (pid) {
            $("#dicPid").val(pid);
        } else {
            $("#dicPid").val('');
        }
        js.modal.open("modal-create");
    }

    /**
     * 打开模态框, 回显数据
     */
    function openEdit(id) {
        js.post({
            url: ctx + "basic/dictionary/id",
            data: {
                id: id
            },
            success: function (result) {
                $("#createForm").fillData(result);
                js.modal.open("modal-create");
                js.modal.closeLoading();
                setTimeout(function () {
                    js.validate.validing("createForm");
                }, 500);
            }
        })
    }

    /**
     * 删除
     */
    function del(id) {
        js.modal.confirm("确认删除此数据类别吗？", function (result) {
            js.post({
                url: ctx + "basic/dictionary/del",
                data: {
                    dicId: id,
                    dicStatus: 0
                },
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.treeTable.refresh(table);
                    } else {
                        js.modal.warning(result.msg);
                    }
                    js.modal.closeLoading();
                }
            })
        })
    }

    $(function () {
        $("title").html($("title").html()+" —— 物料类别维护");
        /**
         * 加载表格
         */
        table = js.treeTable.init({
            url: ctx + "basic/dictionary/list",
            code: "dicId",
            parentCode: "dicPid",
            columns: [
                {
                    title: "名称", field: 'dicName', align: "left", width: '20%',
                    formatter: function (value, row, index) {
                        return '<i class="fa fa-map-o"></i> <span class="nav-label">' + value + '</span>';
                    }
                },
                {title: '值', field: 'dicValue'},
                {title: '类别', field: 'dicType'},
                {title: '备注', field: 'dicReamrk'},
                {title: '排序', field: 'dicOrderNo'},
                {
                    title: '状态', field: 'dicStatus',
                    formatter: function (value, row, index) {
                        if (value === 1) {
                            return '可用';
                        } else if (value === 2) {
                            return '失效';
                        }
                    }
                },
                {
                    title: "操作",
                    width: '250',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="openCreate(' + row.dicId + ')"> 新增</a> ');
                        actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="openEdit(' + row.dicId + ')"> 编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="del(' + row.dicId + ')"> 删除</a>');
                        return actions.join('');
                    }
                }
            ]

        });

        /**
         * 表格搜索
         */
        $("#btn-search").click(function () {
            js.treeTable.search(table);
        });

        /**
         * 验证
         */
        js.validate.init("createForm", {
            fields: {
                dicName: {
                    validators: {
                        notEmpty: {message: '请输入类别名称'},
                        remote: {
                            url: ctx + "basic/dictionary/checkUnique",
                            message: '类别已存在',
                            delay: 200,
                            type: 'POST',
                            data: {
                                dicId: function () {
                                    return $("#dicId").val();
                                }
                            }
                        }
                    }
                },
                dicType: {validators: {notEmpty: {message: '请选择类别'}}},
                dicStatus: {validators: {notEmpty: {message: '请选择状态'}}}
            }
        });

        /**
         * 创建仓库
         */
        $("#btn-create").click(function () {
            var url = ctx + "basic/dictionary/save";
            if ($("#dicId").val()) {
                url = ctx + "basic/dictionary/update";
            }
            if (js.validate.isValid("createForm")) {
                js.post({
                    url: url,
                    data: $("#createForm").serialize(),
                    success: function (result) {
                        if (result.type === web_status.SUCCESS) {
                            js.modal.success(result.msg);
                            js.treeTable.refresh(table);
                            js.modal.hide("modal-create");
                        } else {
                            js.modal.warning(result.msg);
                        }
                        js.modal.closeLoading();
                    }
                })
            }
        });

        /**
         * 模态框隐藏事件， 重置表单，重置验证
         */
        js.modal.hideEvent("modal-create", function () {
            js.validate.reset("createForm");
            js.reset("createForm");
        })
    });

</script>
</body>
</html>
